package com.mobisys.live.controller;

import com.mobisys.live.response.Response;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.auth.COSSigner;
import com.qcloud.cos.http.HttpMethodName;

import com.qiniu.util.Auth;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

import static com.mobisys.live.config.Config.*;
import static com.mobisys.live.config.ResponseCode.*;

/**
 * Created by zhuo on 2018-01-16.
 */
@RestController
public class CosController {

    //获取文件上传的签名
    public void getVodUploadSign() {

/*
        long currentTime = System.currentTimeMillis();
        long expiredTime = currentTime + COSKEY_EXPIRED_TIME;

        $srcStr = 'a=' . strval(COSKEY_APPID) .
        '&b=' . COSKEY_BUCKET .
        '&k=' . COSKEY_SECRECTID .
        '&e=' . $expiredTime .
        '&t=' . $currentTime .
        '&r=' .rand() .
        '&f=';
        $signStr = base64_encode(hash_hmac('SHA1', $srcStr, COSKEY_SECRECTKEY, true). $srcStr);


        $this->_retValue = EC_OK;
        $this->_data=array("sign"=>$signStr);
        interface_log(INFO, EC_OK, 'GetCOSSign::process() succeed.' .  $srcStr . '::' . $signStr);
        return true;*/
    }

//    @RequestMapping(value = "/objectStorage/getUpObjectStorageSign",method = RequestMethod.POST)
//    public Response getUpObjectStorageSign(String key) {
//        // bucket 的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
//        Response response = new Response();
//        String bucketName = "xidianlive-"+COSKEY_APPID;
//        COSCredentials cred = new BasicCOSCredentials(CLOUD_API_SECRETID, CLOUD_API_SECRETKEY);
//        try {
//            COSSigner signer = new COSSigner();
//            //设置过期时间为 1 个小时
//            Date expiredTime = new Date(System.currentTimeMillis() + 3600L * 1000L * 24);
//            // 要签名的 key, 生成的签名只能用于对应此 key 的上传
//            //String key = "/aaa.txt";
//            //key=bucketName+"/"+key;
//            String sign = signer.buildAuthorizationStr(HttpMethodName.PUT, key, cred, expiredTime);
//
//            response.setRetureValue(SUCCESS);
//            response.setRetureMsg(MSG_SUCCESS);
//            response.setRetureData(sign);
//        }catch (Exception e){
//            e.printStackTrace();
//            response.setRetureValue(GET_OBJECT_SIGN_ERROR);
//            response.setRetureMsg(MSG_GET_OBJECT_SIGN_ERROR);
//        }
//        return response;
//    }\

    /**
     * 生成上传对象的时间戳
     * @param key
     * @return
     */
    @RequestMapping(value = "/objectStorage/getUpObjectStorageSign",method = RequestMethod.POST)
    public Response getUpObjectStorageSign(String key){

        Response response = new Response();
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
//        key = key+df.format(System.currentTimeMillis());
        Auth auth =Auth.create(AK,SK);
        try {
        String token = auth.uploadToken(bucket,key);
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(token);
        }catch (Exception e){
            e.printStackTrace();
            response.setRetureValue(GET_OBJECT_SIGN_ERROR);
            response.setRetureMsg(MSG_GET_OBJECT_SIGN_ERROR);
        }
        return response;
    }
}
